package lyx.tank.engine
{
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	import flash.utils.getTimer;
	
	import lyx.game.GameEngine;
	import lyx.game.GameFramework;
	import lyx.game.control.KeyBoardController;
	import lyx.game.element.DynamicGameElement;
	import lyx.game.element.GameElement;
	import lyx.game.locator.ElementLocator;
	import lyx.game.utils.Direction;
	import lyx.game.utils.GameState;
	import lyx.tank.Main;
	import lyx.tank.element.AITank;
	import lyx.tank.element.Bullet;
	import lyx.tank.element.PlayerTank;
	import lyx.tank.element.Tank;
	import lyx.tank.element.TankBase;
	import lyx.tank.engine.logic.BulletLogic;
	import lyx.tank.engine.logic.TankLogic;
	import lyx.tank.stages.GameStageInfo_1;
	import lyx.tank.stages.GameStageInfo_2;
	import lyx.tank.stages.StageInfo;
	import lyx.tank.view.GameWorldView;

	public class TankGameEngine extends GameEngine
	{
		private var _tank_logic:TankLogic = new TankLogic();
		private var _bullet_logic:BulletLogic = new BulletLogic();
		
		private var _current_stage:StageInfo;
		private var _stages:Array = [new GameStageInfo_1(),new GameStageInfo_2()];
		
		private var _tank_base:TankBase;
		private var _player_tank:PlayerTank;
		private var _waiting_ai_tank:AITank;
		
		private var _ai_tank_number:int = 0;
		public function get ai_tank_number():int
		{
			return _ai_tank_number;
		}
		
		/**
		 * TankGame：game_state=GameState.GAME_START的处理函数
		 */
		override protected function gameStartHandler():void
		{
			_current_stage = _stages.shift();
			var info_data:XML = _current_stage.data;
			
			var birthplace_data:XMLList = info_data.birthplace;
			
			_player_tank = new PlayerTank(birthplace_data.playertank.@x,birthplace_data.playertank.@y);
			_player_tank.active_area = new Rectangle(0,0,480,480);
			_player_tank.direction = Direction.NORTH;
			ElementLocator.GAME_ELEMENTS.push(_player_tank);
			GameFramework.getInstance().keyboard_controller.addTarget(_player_tank);
			
			_ai_tank_number = 0;
			for each(var xml:XML in birthplace_data.aitank)
			{
				_ai_tank_number += int(xml.@output);
			}
			GameWorldView(GameFramework.getInstance().world).updateInfoPanel();
			
			for each(var element_info:XML in info_data.elements.game_element)
			{
				var class_name:String = element_info.@class_name;
				var element_class:Class = StageInfo(_current_stage).getElementDefinition(class_name);
				var game_element:GameElement = new element_class();
				game_element.x = element_info.@x;
				game_element.y = element_info.@y;
				ElementLocator.GAME_ELEMENTS.push(game_element);
				if(game_element is TankBase)
				{
					_tank_base = game_element as TankBase;
				}
			}
			
			switchGameState(GameState.GAME_RUNNING);
		}
		
		/**
		 * TankGame：game_state=GameState.GAME_STARTED的处理函数
		 */
		override protected function gameRunningHandler():void
		{
			var time:int = getTimer();
			_time = time - _time;
			
			GameFramework.getInstance().keyboard_controller.addTarget(_player_tank);
			
			if(_waiting_ai_tank==null)
			{
				_waiting_ai_tank = new AITank();
				_waiting_ai_tank.active_area = new Rectangle(0,0,480,480);
			}
			
			var info_data:XML = _current_stage.data;
			var birthplace_data:XMLList = info_data.birthplace;
			
			var birthplace_blocked:Boolean = false;
				
			for each(var element:GameElement in ElementLocator.GAME_ELEMENTS)
			{
				if(element is Tank)
				{
					_tank_logic.handle(_time,element,ElementLocator.GAME_ELEMENTS);
					for each(var xml:XML in birthplace_data.aitank)
					{
						_waiting_ai_tank.x = int(xml.@x);
						_waiting_ai_tank.y = int(xml.@y);
						birthplace_blocked = _waiting_ai_tank.hitTest(element);
						if(birthplace_blocked)
						{
							break;
						}
					}
				}
				else if(element is Bullet)
				{
					_bullet_logic.handle(_time,element,ElementLocator.GAME_ELEMENTS);
				}
			}
			
			if(!birthplace_blocked)
			{
				var birth_num:int = XMLList(birthplace_data.aitank).length();
				var birth_xml:XML = birthplace_data.aitank[int(Math.random()*10)%2];
				_waiting_ai_tank.x = birth_xml.@x;
				_waiting_ai_tank.y = birth_xml.@y;
				ElementLocator.GAME_ELEMENTS.push(_waiting_ai_tank);
				_waiting_ai_tank = null;
			}
			
			var length:int = ElementLocator.GAME_ELEMENTS.length;
			for(var i:int=0;i<length;i++)
			{
				var item:GameElement = ElementLocator.GAME_ELEMENTS[i];
				if(item.life==0)
				{
					ElementLocator.GAME_ELEMENTS.splice(i,1);
					if(item is AITank)
					{
						_ai_tank_number--;
						GameWorldView(GameFramework.getInstance().world).updateInfoPanel();
					}
					length--;
					i--;
				}
			}
			
			if(_player_tank.life==0||_tank_base.life==0||ai_tank_number==0)
			{
				switchGameState(GameState.GAME_OVER);
			}
			
			_time = time;
		}
		
		override protected function gamePauseHandler():void
		{
			GameFramework.getInstance().keyboard_controller.removeTarget(_player_tank);
		}
		
		override protected function gameOverHandler():void
		{
			GameFramework.getInstance().keyboard_controller.removeTarget(_player_tank);
			
			ElementLocator.GAME_ELEMENTS = [];
			
			if(_stages.length!=0)
			{
				switchGameState(GameState.GAME_START);
			}
		}
		
	}
}